//为数组拓展排序方法:从大到小
let arr = new Array(1, 4, 12, 16, 20, 25, 8, 3)
let arr1 = [4, 16, 12, 25, 27, 7, 10, 15]
//true表示升序,false表示降序
Array.prototype.sortArray = function (bool = false) {
    for (let i = 0; i < this.length - 1; i++) {
        for (let j = 0; j < this.length - 1 - i; j++) {
            if (bool) {
                if (this[j] > this[j + 1]) {
                    let temp = this[j]
                    this[j] = this[j + 1]
                    this[j + 1] = temp
                }
            } else {
                if (this[j] < this[j + 1]) {
                    let temp = this[j]
                    this[j] = this[j + 1]
                    this[j + 1] = temp
                }
            }
        }
    }
    return this
}
console.log(arr.sortArray(false)) //降序
console.log(arr1.sortArray(true)) //升序








// 原生方法：
//方法一：
var arr = [9, 23, 16, 7, 65, 4, 5, 45];
var temp;
for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length; j++) {
        if (arr[i] < arr[j]) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}
document.write(arr);
//方法二：
// arr.sort(function(n1,n2){
// 	return n1-n2
// });
// document.write(arr);